package com.example.demo.service;

import com.example.demo.domain.Message;
import com.example.demo.domain.repository.MessageRepository;
import com.example.demo.domain.repository.TicketRepository;
import com.example.demo.domain.Ticket;
import com.example.demo.util.DateUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TicketService {
    @Autowired
    private TicketRepository ticketRepository;

    @Autowired
    private MessageRepository messageRepository;
    //    获取对应审批票据列表相关方法
    //
    //
    //
    //
    /**
     * @Description: 获取对应审批列表
     * @Param:  keyword: 搜索关键字
     *          process: 审核进程
     * @return:  全部未审批列表
     * @Author: wangze
     */
    public List<Ticket> getAllUncheckedTicket(String keyword,Integer process) {
        return ticketRepository.findByEmployeeNameContainingAndCheckProcessOrDepartmentContainingAndCheckProcessOrderByDateAsc(
                keyword,process,
                keyword,process
        );
    }

    /**
     * @Description: 按照职工姓名搜索对应审批票据
     * @Param: keyword: 搜索关键字
     *          process: 审核进程
     * @return: 特定列表
     * @Author: wangze
     */
    public List<Ticket> getUncheckedTicketByEmployeeName(String keyword,Integer process){
        return ticketRepository.findByEmployeeNameContainingAndCheckProcessOrderByDateAsc(keyword,process);
    }

    /**
     * @Description: 按照员工部门搜索对应审批票据
     * @Param:  keyword: 搜索关键字
     *          process: 审核进程
     * @return:  特定列表
     * @Author: wangze
     */
    public List<Ticket> getUncheckedTickerByDepartment(String keyword,Integer process){
        return ticketRepository.findByDepartmentContainingAndCheckProcessOrderByDateAsc(keyword,process);
    }

    //     获取审核中票据列表相关方法
    //
    //
    //
    //
    /**
     * @Description: 获取审核中票据列表
     * @Param:  keyword: 搜索的关键字
     * @return:  审核中票据列表
     * @Author: wangze
     */
    public List<Ticket> getCheckingTickets(String keyword) {
        return ticketRepository.findByDepartmentContainingAndCheckProcessNotOrEmployeeNameContainingAndCheckProcessNotOrderByDateDesc(
                keyword,3,
                keyword,3);
    }

    /**
     * @Description: 按照职工姓名搜索审核中票据列表
     * @Param: keyword: 搜索关键字
     * @return: 特定列表
     * @Author: wangze
     */
    public List<Ticket> getCheckingTicketsByEmployeeName(String keyword){
        return ticketRepository.findByEmployeeNameContainingAndCheckProcessNotOrderByDateDesc(keyword,3);
    }

    /**
     * @Description: 按照员工部门搜索审核中票据列表
     * @Param:  keyword: 搜索关键字
     * @return:  特定列表
     * @Author: wangze
     */
    public List<Ticket> getCheckingTicketsByDepartment(String keyword){
        return ticketRepository.findByDepartmentContainingAndCheckProcessNotOrderByDateDesc(keyword,3);
    }


    //     获取已通过票据列表相关方法
    //
    //
    //
    //
    /**
     * @Description: 获取已通过票据列表
     * @Param:  keyword: 搜索的关键字
     * @return:  已通过票据列表
     * @Author: wangze
     */
    public List<Ticket> getPassedTickets(String keyword) {
        return ticketRepository.findByDepartmentContainingAndCheckProcessAndStateOrEmployeeNameContainingAndCheckProcessAndStateOrderByDateDesc(
                keyword,3,"通过",
                keyword,3,"通过");
    }

    /**
     * @Description: 按照职工姓名搜索已通过票据列表
     * @Param: keyword: 搜索关键字
     * @return: 特定列表
     * @Author: wangze
     */
    public List<Ticket> getPassedTicketsByEmployeeName(String keyword){
        return ticketRepository.findByEmployeeNameContainingAndCheckProcessAndStateOrderByDateDesc(keyword,3,"通过");
    }

    /**
     * @Description: 按照员工部门搜索已通过票据列表
     * @Param:  keyword: 搜索关键字
     * @return:  特定列表
     * @Author: wangze
     */
    public List<Ticket> getPassedTicketsByDepartment(String keyword){
        return ticketRepository.findByDepartmentContainingAndCheckProcessAndStateOrderByDateDesc(keyword,3,"通过");
    }



    //     获取已驳回票据列表相关方法
    //
    //
    //
    //
    /**
     * @Description: 获取已驳回票据列表
     * @Param:  keyword: 搜索的关键字
     * @return:  已通过票据列表
     * @Author: wangze
     */
    public List<Ticket> getRefusedTickets(String keyword) {
        return ticketRepository.findByDepartmentContainingAndCheckProcessAndStateOrEmployeeNameContainingAndCheckProcessAndStateOrderByDateDesc(
                keyword,3,"驳回",
                keyword,3,"驳回");
    }

    /**
     * @Description: 按照职工姓名搜索已驳回票据列表
     * @Param: keyword: 搜索关键字
     * @return: 特定列表
     * @Author: wangze
     */
    public List<Ticket> getRefusedTicketsByEmployeeName(String keyword){
        return ticketRepository.findByEmployeeNameContainingAndCheckProcessAndStateOrderByDateDesc(keyword,3,"驳回");
    }

    /**
     * @Description: 按照员工部门搜索已驳回票据列表
     * @Param:  keyword: 搜索关键字
     * @return:  特定列表
     * @Author: wangze
     */
    public List<Ticket> getRefusedTicketsByDepartment(String keyword){
        return ticketRepository.findByDepartmentContainingAndCheckProcessAndStateOrderByDateDesc(keyword,3,"驳回");
    }


    //   票据增删改查相关方法
    //
    //
    //
    //
    /**
     * @Description:  增加票据
     * @Param:  ticket 票据实体
     * @return:  添加结果
     * @Author: wangze
     */
    public Ticket addTicket(Ticket ticket) {
        ticket.setDate(new Date());
        ticket.setCheckProcess(0);
        ticket.setState("预算科审核中");
        return ticketRepository.save(ticket);
    }

    /**
     * @Description: 根据ID删除票据
     * @Param:  id：票据ID
     * @return:  无
     * @Author: wangze
     */
    public void deleteTicketById(Integer id){
        ticketRepository.deleteTicketById(id);
    }

    /**
     * @Description: 根据ID获取票据
     * @Param:  id：票据ID
     * @return:  票据实体
     * @Author: wangze
     */
    public Ticket getTicketById(Integer id){
        return ticketRepository.getTicketById(id);
    }

    /**
     * @Description: 通过审核
     * @Param: id 票据ID
     * @Param: process 审核流程
     * @Param: auditor 审核人
     * @Param: opinion 审核意见
     * @return:
     * @Author: wangze
     */
    public Ticket pass(Integer id,Integer process,String auditor,String opinion){
        Ticket ticket=ticketRepository.getTicketById(id);
        if(ticket==null)
            return null;
        if(process==0){
            ticket.setState("部门经理审核");
            ticket.setAuditor1(auditor);
            ticket.setOpinion1(opinion);
            ticket.setDate1(new Date());
        }
        else if(process==1){
            ticket.setState("财务处审核");
            ticket.setAuditor2(auditor);
            ticket.setOpinion2(opinion);
            ticket.setDate2(new Date());
        }
        else{
            ticket.setState("通过");
            ticket.setAuditor3(auditor);
            ticket.setOpinion3(opinion);
            ticket.setDate3(new Date());
        }
        ticket.setCheckProcess(process+1);
        Ticket temp=ticketRepository.save(ticket);
        if(process==2){
            Message message = new Message();
            message.setState(false);
            message.setWorkNumber(ticket.getWorkNumber());
            message.setDate(new Date());
            message.setIntroduction("审核通过");
            message.setReceiptId(temp.getId());
            messageRepository.save(message);

        }
        return temp;
    }

    /**
     * @Description: 驳回审核
     * @Param: id: 票据ID
     *         process: 审核进程
     *         auditor: 审核人
     *         opinion: 审核意见
     * @return:
     * @Author: wangze
     */
    public Ticket refuse(Integer id,Integer process,String auditor,String opinion){
        Ticket ticket=ticketRepository.getTicketById(id);
        if(ticket==null)
            return null;
        if(process==0){
            ticket.setAuditor1(auditor);
            ticket.setOpinion1(opinion);
            ticket.setDate1(new Date());
        }
        else if(process==1){
            ticket.setAuditor2(auditor);
            ticket.setOpinion2(opinion);
            ticket.setDate2(new Date());
        }
        else{
            ticket.setAuditor3(auditor);
            ticket.setOpinion3(opinion);
            ticket.setDate3(new Date());
        }
        ticket.setCheckProcess(3);
        ticket.setState("驳回");
        Ticket temp = ticketRepository.save(ticket);
        Message message = new Message();
        message.setState(false);
        message.setWorkNumber(ticket.getWorkNumber());
        message.setDate(new Date());
        message.setIntroduction("审核驳回");
        message.setReceiptId(temp.getId());
        messageRepository.save(message);
        return temp;
    }

    /**
     * @Description: 返回职工已审核单据列表给微信小程序
     * @Param: worknumber: 职工工号
     * @return: 对应票据列表
     * @Author: wangze
     */
    public List<Ticket> findCheckedTicketByWorkNumber(String workNumber){
        return ticketRepository.findByWorkNumberAndCheckProcessOrderByDateDesc(workNumber,3);
    }

    /**
     * @Description: 返回职工审核中单据列表给微信小程序
     * @Param: worknumber: 职工工号
     * @return: 对应票据列表
     * @Author: wangze
     */
    public List<Ticket> findUncheckedTicketByWorkNumber(String workNumber){
        return ticketRepository.findByWorkNumberAndCheckProcessNotOrderByDateDesc(workNumber,3);
    }

    /**
     * @Description: 获取某一年每月的报销额度
     * @param year 年份
     * @return: 报销额度数组
     * @Author: wangze
     */
    public List<Integer> getMonthlyAmount(Integer year){
        List<Ticket> ticketList = ticketRepository.findByCheckProcessAndStateOrderByDateDesc(3,"通过");
        Date month1First = DateUtil.getFirstDay(year,1);
        Date month1Last = DateUtil.getLastDay(year,1);
        Date month2First = DateUtil.getFirstDay(year,2);
        Date month2Last = DateUtil.getLastDay(year,2);
        Date month3First = DateUtil.getFirstDay(year,3);
        Date month3Last = DateUtil.getLastDay(year,3);
        Date month4First = DateUtil.getFirstDay(year,4);
        Date month4Last = DateUtil.getLastDay(year,4);
        Date month5First = DateUtil.getFirstDay(year,5);
        Date month5Last = DateUtil.getLastDay(year,5);
        Date month6First = DateUtil.getFirstDay(year,6);
        Date month6Last = DateUtil.getLastDay(year,6);
        Date month7First = DateUtil.getFirstDay(year,7);
        Date month7Last = DateUtil.getLastDay(year,7);
        Date month8First = DateUtil.getFirstDay(year,8);
        Date month8Last = DateUtil.getLastDay(year,8);
        Date month9First = DateUtil.getFirstDay(year,9);
        Date month9Last = DateUtil.getLastDay(year,9);
        Date month10First = DateUtil.getFirstDay(year,10);
        Date month10Last = DateUtil.getLastDay(year,10);
        Date month11First = DateUtil.getFirstDay(year,11);
        Date month11Last = DateUtil.getLastDay(year,11);
        Date month12First = DateUtil.getFirstDay(year,12);
        Date month12Last = DateUtil.getLastDay(year,12);
        int amount1=0,amount2=0,amount3=0,amount4=0,amount5=0,amount6=0,
                amount7=0,amount8=0,amount9=0,amount10=0,amount11=0,amount12=0;
        for(Ticket temp:ticketList){
            if(DateUtil.isBetweenDates(temp.getDate(),month1First,month1Last)){
                amount1 += temp.getReport().intValue();
            }
            if(DateUtil.isBetweenDates(temp.getDate(),month2First,month2Last)){
                amount2 += temp.getReport().intValue();
            }
            if(DateUtil.isBetweenDates(temp.getDate(),month3First,month3Last)){
                amount3 += temp.getReport().intValue();
            }
            if(DateUtil.isBetweenDates(temp.getDate(),month4First,month4Last)){
                amount4 += temp.getReport().intValue();
            }
            if(DateUtil.isBetweenDates(temp.getDate(),month5First,month5Last)){
                amount5 += temp.getReport().intValue();
            }
            if(DateUtil.isBetweenDates(temp.getDate(),month6First,month6Last)){
                amount6 += temp.getReport().intValue();
            }
            if(DateUtil.isBetweenDates(temp.getDate(),month7First,month7Last)){
                amount7 += temp.getReport().intValue();
            }
            if(DateUtil.isBetweenDates(temp.getDate(),month8First,month8Last)){
                amount8 += temp.getReport().intValue();
            }
            if(DateUtil.isBetweenDates(temp.getDate(),month9First,month9Last)){
                amount9 += temp.getReport().intValue();
            }
            if(DateUtil.isBetweenDates(temp.getDate(),month10First,month10Last)){
                amount10 += temp.getReport().intValue();
            }
            if(DateUtil.isBetweenDates(temp.getDate(),month11First,month11Last)){
                amount11 += temp.getReport().intValue();
            }
            if(DateUtil.isBetweenDates(temp.getDate(),month12First,month12Last)){
                amount12 += temp.getReport().intValue();
            }
        }
        Integer integer1 = new Integer(amount1);
        Integer integer2 = new Integer(amount2);
        Integer integer3 = new Integer(amount3);
        Integer integer4 = new Integer(amount4);
        Integer integer5 = new Integer(amount5);
        Integer integer6 = new Integer(amount6);
        Integer integer7 = new Integer(amount7);
        Integer integer8 = new Integer(amount8);
        Integer integer9 = new Integer(amount9);
        Integer integer10 = new Integer(amount10);
        Integer integer11 = new Integer(amount11);
        Integer integer12 = new Integer(amount12);
        List<Integer> integerList = new ArrayList<>();
        integerList.add(integer1);
        integerList.add(integer2);
        integerList.add(integer3);
        integerList.add(integer4);
        integerList.add(integer5);
        integerList.add(integer6);
        integerList.add(integer7);
        integerList.add(integer8);
        integerList.add(integer9);
        integerList.add(integer10);
        integerList.add(integer11);
        integerList.add(integer12);
        return integerList;
    }

    /**
     * @Description: 获取某一年各报销额度范围占比
     * @param year 年份
     * @return: 各报销范围的单据数量
     * @Author: wangze
     */
    public String getRatio(Integer year){
        List<Ticket> ticketList = ticketRepository.findByCheckProcessAndStateOrderByDateDesc(3,"通过");
        Date firstDay = DateUtil.getYearFirst(year);
        Date lastDay = DateUtil.getYearLast(year);
        int amount1=0,amount2=0,amount3=0,amount4=0;
        for (Ticket temp:ticketList){
            if(DateUtil.isBetweenDates(temp.getDate(),firstDay,lastDay)){
                if(temp.getReport().intValue()<5000)
                    amount1++;
                else if(temp.getReport().intValue()<10000)
                    amount2++;
                else if (temp.getReport().intValue()<50000)
                    amount3++;
                else
                    amount4++;
            }
        }
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("name","5000以下");
        jsonObject1.put("value",amount1);
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("name","5000—10000");
        jsonObject2.put("value",amount2);
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("name","10000—50000");
        jsonObject3.put("value",amount3);
        JSONObject jsonObject4 = new JSONObject();
        jsonObject4.put("name","50000以上");
        jsonObject4.put("value",amount4);
        jsonArray.add(jsonObject1);
        jsonArray.add(jsonObject2);
        jsonArray.add(jsonObject3);
        jsonArray.add(jsonObject4);
        return jsonArray.toString();
    }

}
